package com.xiaoq;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xiaoq.users.service.IMUserReceiver;

@Configuration
@EnableConfigurationProperties({RabbitMQConnectionProperties.class})
public class RabbitMQConfig {
	@Autowired
	private RabbitMQConnectionProperties connectionConfig;
	
	@Autowired
	private ConnectionFactory connectionFactory;

	@Autowired
	private IMUserReceiver imUserReceiver;
	
	@Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(connectionConfig.getHost(), connectionConfig.getPort());
        connectionFactory.setUsername(connectionConfig.getUserName());
        connectionFactory.setPassword(connectionConfig.getPassword());
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

	@Bean
	public RabbitTemplate rabbitTemplate() {
		RabbitTemplate tpl = new RabbitTemplate();
		tpl.setConnectionFactory(connectionFactory);
		return tpl;
	}
	
	@Bean
	public Queue imUserQueue() {
		return new Queue("xiaoq.im.user", true);
	}
	
	@Bean
	public TopicExchange topicExchange() {
		return new TopicExchange("xiaoq-im-exchange");
	}
	
	@Bean
	public FanoutExchange fanoutExchange() {
		return new FanoutExchange("xiaoq-im-exchange-fanout");
	}
	
	@Bean
    public SimpleRabbitListenerContainerFactory myFactory(
    		SimpleRabbitListenerContainerFactoryConfigurer configurer) {
		SimpleRabbitListenerContainerFactory factory =
                new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
	}

	@Bean 
	public Binding imUserBinding() {
		Queue imUserQueue = imUserQueue();
		return BindingBuilder.bind(imUserQueue).to(topicExchange()).with(imUserQueue.getName());
	}
	
	@Bean
	public SimpleMessageListenerContainer contentPubContainer() {
		return build(imUserQueue(), imUserReceiver);
	}
	
	private SimpleMessageListenerContainer build(Queue queue, AbstractReceiver recv) {
		SimpleMessageListenerContainer lc = new SimpleMessageListenerContainer();
		lc.setConnectionFactory(connectionFactory);
		lc.setQueues(queue);
		MessageListenerAdapter la = new MessageListenerAdapter(recv);
		lc.setMessageListener(la);
		lc.setConcurrentConsumers(1);
		return lc;
	}

	
}
